package com.ktjy.t382.ch01.controller;
import com.ktjy.t382.ch01.pojo.BookManage;
import com.ktjy.t382.ch01.pojo.Page;
import com.ktjy.t382.ch01.service.BookManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "*")
@Controller
@RequestMapping("/bookManage")
public class BookManageController {
    @Autowired
    BookManageService bookManageService;

    @ResponseBody
    @RequestMapping("/listBook")
    public List<BookManage> list(Model model) {
        List<BookManage> listBook = bookManageService.getListBiz(new BookManage());
        model.addAttribute("listBook", listBook);
        return listBook;
    }

    @ResponseBody
    @GetMapping("/deleteBiz")
    public int deleteBiz(@RequestParam Integer id) {
        int biz = bookManageService.deleteBiz(id);
        return biz;
    }

    @ResponseBody
    @PostMapping("/addBiz")
    public int addBiz(@RequestBody BookManage bookManage) {
        int insertBiz = bookManageService.insertBiz(bookManage);
        return insertBiz;
    }

    @RequestMapping("/addBook")
    public String addBook() {
        return "addBook";
    }

    @ResponseBody
    @GetMapping("/viewBook")
    public BookManage viewBook(@RequestParam Integer id, Model model) {
        BookManage bookManage = bookManageService.getByIdBiz(id);
        model.addAttribute("bookManage", bookManage);
        return bookManage;
    }

//    @ResponseBody
//    @GetMapping("/toBook")
//    public BookManage toBook(@RequestParam Integer id, Model model) {
//        BookManage bookManage = bookManageService.getByIdBiz(id);
//        model.addAttribute("bookManage", bookManage);
//        return bookManage;
//    }

    @ResponseBody
    @PostMapping("/updateBook")
    public int updateBook(@RequestBody BookManage bookManage) {
        int updateBiz = bookManageService.updateBiz(bookManage);
        return updateBiz;
    }

    @ResponseBody
    @RequestMapping("/page")
    public Page page(Model model,
                     @RequestParam Integer pageNo,
                     @RequestParam Integer pageSize, @RequestParam String name) {
        Page page = bookManageService.getPage(pageNo, pageSize, name);
        model.addAttribute("page", page);
        return page;
    }
}
